package com.sichen.search.controller;

import com.sichen.common.exception.BizCodeEnum;
import com.sichen.common.to.es.SkuEsModel;
import com.sichen.common.utils.R;
import com.sichen.search.service.ProductSaveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * 对ES进行保存操作的所有方法
 * <p>Created with IntelliJ IDEA.
 * <p>Author: 思尘
 * <p>Created time: 2022-09-12 16:07
 * <p>Version: V1.0
 */
@Slf4j
@RestController
@RequestMapping("/search/save")
public class ElasticSearchSaveController {

    @Autowired
    ProductSaveService productSaveService;

    /**
     * 上架商品
     * @param skuEsModels Sku的模型
     * @return 上架结果
     */
    @PostMapping("/product")
    public R productStatusUp(@RequestBody List<SkuEsModel> skuEsModels){
        boolean b = false;
        try {
            b = productSaveService.productStatusUp(skuEsModels);
        } catch (IOException e) {
            log.error("ElasticSearchSaveController商品上架错误:{}",e.getMessage());
            return R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(),BizCodeEnum.PRODUCT_UP_EXCEPTION.getMessage());
        }
        return !b? R.ok():R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(),BizCodeEnum.PRODUCT_UP_EXCEPTION.getMessage());
    }
}
